﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farigola.Engine.Factories;
using NUnit.Framework;
using Farigola.Engine.SystemData.Entity;

namespace Farigola.Engine.TestUnit
{
    /// <summary>
    /// Test the entity id class.
    /// </summary>
     [TestFixture]
    public class EntityIdTestUnit
    {
         	/**
	 * create an entityId object not set.
	 */
	[Test]
	public void testIdNotSet() {
		// create an id
		EntityId entityId = new EntityId();
		
		String idStr=entityId.ToString();
		Assert.AreEqual("(null)",idStr,"The id should be null.");
	}
	
	/**
	 * The standard case, generate a new id.
	 */
	[Test]
	public void testNewId() {
		// create an id
		EntityId entityId = new EntityId(true);
		
		String idStr=entityId.ToString();
		Assert.NotNull(idStr, "The id should exists, should be not null.");
		
		// check that the UUID id is right
		Guid id = new Guid(entityId.ToString());
		Assert.IsNotNull(id, "The UUID id should exists, should be not null.");		
	}

	/**
	 * Create a new id from an existing id in string format.
	 */
	[Test]
	public void testNewIdFromIdStr() {		
		// create an id from an existing id in string format
		EntityId entityId = new EntityId();
		
		Guid id= Guid.NewGuid();
		
		bool res= entityId.SetFromIdStr(id.ToString());
		Assert.AreEqual(true, res, "The setFromIdStr should run.");
		
		// compare the id created from an existing id string		
		Assert.AreEqual(id.ToString(), entityId.ToString(), "Id created from an existing id string is wrong.");		
	}
	
	/**
	 * Create a new id from an existing id in string format.
	 * but the id string is wrong.
	 */
	[Test]
	public void testNewIdFromWrongIdStr() {
		// create an id from an existing id in string format
		EntityId entityId = new EntityId();
				
		bool res= entityId.SetFromIdStr("wrong-id");
        Assert.AreEqual(false, res,"The setFromIdStr should failed.");
	}
	
	/**
	 * try to change the id, but its not possible
	 */
	[Test]
	public void testTryChangeIdFromIdStr() {		
		// create an id from an existing id in string format
		EntityId entityId = new EntityId(true);
		Guid initialId= new Guid(entityId.ToString()); 
				
		Guid newId= Guid.NewGuid();		
		bool res= entityId.SetFromIdStr(newId.ToString());
        Assert.AreEqual(false, res, "The setFromIdStr should failed.");
		
		// compare the id , must constains the initial id		
        Assert.AreEqual(initialId.ToString(), entityId.ToString(), "Initial Id should be not changed");		
	}
	
	[Test]
	public void testIdEquals() {
		EntityId entityId1 = new EntityId();
		EntityId entityId2 = new EntityId();

		Guid newId= Guid.NewGuid();		
		entityId1.SetFromIdStr(newId.ToString());
		entityId2.SetFromIdStr(newId.ToString());

        bool res = entityId1.IdAreEquals(entityId2);
        Assert.IsTrue(res, "Id1 and Id2 are equals.");
	}
	
	[Test]
	public void testIdNotEquals() {
		EntityId entityId1 = new EntityId();
		EntityId entityId2 = new EntityId();

		Guid newId= Guid.NewGuid();		
		entityId1.SetFromIdStr(newId.ToString());
		newId= Guid.NewGuid();		
		entityId2.SetFromIdStr(newId.ToString());

		Assert.IsFalse(entityId1.IdAreEquals(entityId2),"Id1 and Id2 are not equals.");
	}
		// create an id from an existing id in string format
		//EntityId entityId1 = new EntityId();

    }
}
